#include"MyString.h"

#include<iostream>

#include<cstring>



MyString::MyString():size(0),capacity(15)
{
    this->string = new char(15);
}


MyString::MyString(const char *str)
{   
    this->size = strlen(str);
    capacity = this->size+1; 
    this->string = new char[this->capacity];   
    strcpy(this->string,str);
}


MyString::MyString(const MyString &s)
{
    this->size = s.size;
    this->capacity = s.capacity;  
    this->string = new char[this->capacity];  

    strcpy(this->string,s.string);  

}



MyString &MyString::operator=(const MyString &s)
{
    delete []this->string;
    this->size = s.size;
    this->capacity = s.capacity;
    
    char* newP = new char[this->capacity];
    
    
    this->string = newP;  

    strcpy(this->string,s.string);  
   
    return *this;
}



MyString& MyString::operator+=(const MyString &s)
{
    this->size += s.size;

    if(this->size >=this->capacity)
    {
        this->capacity *=2;
        char* newP = new char[this->capacity];
        memset(newP,0,capacity);

        strcpy(newP,this->string);
        delete []this->string;
        
        this->string = newP;
    } 
    strcat(this->string,s.string);

    return *this;

}

MyString &MyString::operator+=(const char &c)
{
    if(this->size + 1 >= this->capacity)
    {
        this->capacity *= 2;
        char* newA = new char[this->capacity];
        memset(newA,0,capacity);
        strcpy(newA,this->string);

        delete []this->string;

        this->string = newA; 
    }
    this->string[size++] = c;
    this->string[size] = '\0';
    return *this; 
    
}

  

MyString MyString::operator+(const MyString &s)
{
    MyString result = *this;
    result += s;

    return result;
}







MyString &MyString::operator-=(const MyString &s)
{
    char* dest = strstr(this->string,s.string);
    while(dest)
    {
         char* end = dest + s.size;
         while(*end != '\0')
         {
            *dest++ = *end++;
         }
         *dest = '\0';
         dest = strstr(this->string,s.string);
    }
    return *this;
}


MyString MyString::operator-(const MyString &s)
{   
    MyString result = *this;
    result -=s;

    return result;
}



MyString::~MyString()
{
    delete []this->string;
}

void MyString::clear(MyString &s)
{
    memset(s.string,0,s.capacity);
    s.size = 0;
}

bool MyString::operator==(const MyString &s)
{    
    return strcmp(this->string,s.string) == 0;
}


bool MyString::operator&&(const MyString &s)
{  
    return (this->size && s.size);
}


char &MyString::operator[](int index)
{
    return this->string[index];
}

MyString::operator int()
{
    return atoi(this->string);
}


MyStringArray MyString::split(const MyString &delim)
{
    MyString temp = *this;
    
    MyStringArray result;

    char* dest = strtok(temp.string,delim.string);
    
    while(dest)
    {
        result += dest;

        char* dest = strtok(nullptr,delim.string);
    }
    
    return result;
}





std::ostream &operator<<(std::ostream &os, const MyString &s)
{
    std::cout<<s.string;;
    return os;
}

std::istream &operator>>(std::istream &is, MyString &s)
{
    s.clear(s);
    char c = 0;
    while ((c = getchar()) != '\n')  //从终端读取一个字符
    {
        s +=c;
    }
    return is;
}

std::ostream &operator<<(std::ostream &os, const MyStringArray &array)
{
    for(int i=0; i < array.size ; i++)
    {
        std::cout<<array.array[i];;
    }
    return os;
}

std::istream &operator>>(std::istream &is,  MyStringArray &array)
{
    MyString str;
    char c = 0;
    while ((c = getchar()) != '\n')
    {
        str += c;
    }
    array.array[array.size++] += str;
    return is;
}

MyStringArray::MyStringArray():size(0),capacity(15)  //数组的无参构造函数
{
    array = new MyString[15];
}


MyStringArray::MyStringArray(const MyStringArray &array)
{
    this->size = array.size;
    this->capacity = array.capacity;
    
    this->array = new MyString[this->capacity];
    
    memset(this->array,0,this->capacity);
       
    for(int i=0; i<this->size;i++)
    {
        this->array[i] =array.array[i];
    } 
}

MyStringArray::~MyStringArray()
{

    delete []this->array;
}


MyStringArray &MyStringArray::operator+=(const MyString &str)
{
    if(this->size + 1 > this->capacity)
    {
        this->capacity *= 2;
        MyString* newarray = new MyString[this->capacity];
        
        for(int i=0; i<this->size;i++)
        {
            newarray[i] = this->array[i];
        }
        delete []this->array;
        array = newarray;    
    }
    this->array[size++] = str;
    return *this;
}


MyStringArray MyStringArray::operator+(const MyString &str)
{
    MyStringArray result;
    result += str;
    return result;
}








void MyStringArray::show()
{
    for(int i=0;i < this->size;i++)
    {
        std::cout<<this->array[i];
    }
}



MyStringArray &MyStringArray::operator=(const MyStringArray &array)
{
    this->size = array.size;
    this->capacity = array.capacity;
    MyString* newarray = new MyString[this->capacity];
    
    delete []this->array;
    
    this->array = newarray;  
    
    for(int i=0;i<this->size;i++)
    {
        this->array[i] = array.array[i];
    }

    return *this;
}

MyStringArray &MyStringArray::operator-=(const MyString &str)
{
    for(int i=0;i<this->size;i++)
    {
        if(strcmp(this->array[i].string,str.string)==0)
        for(int j=i;j<this->size;j++)
        {
            this->array[j] = this->array[j+1];
            
        }
        this->size--;

    }
    return *this;
    
}

MyStringArray MyStringArray::operator-(const MyString &str)
{
    MyStringArray result = *this;
    result -= str;
    return result;
}

MyString &MyStringArray::operator[](int index)
{
    return this->array[index];
}
